#include <tsin/display/TouhouFont.hpp>

#include <tsin/display/Display.hpp>
#include <tsin/display/DecoratedImage.hpp>

using namespace tsin;

struct FontSizeHelper {
        int w;
        int h;
        int (*x)(char character, int w, int h);
        int (*y)(char character, int w, int h);
};

static int x_14pt(char character, int w, int h)
{
        if (character < ' ' || character > '~')
                return 98;
        int idx = character - ' ';
        return 14 * (idx%18);
}

static int y_14pt(char character, int w, int h)
{
        if (character < ' ' || character > '~')
                return 172;
        int idx = character - ' ';
        return 256 - 14 * (idx/18 + 1);
}

static FontSizeHelper helpers[TouhouFont::_FONTSIZE_END] = {
        {14, 14, x_14pt, y_14pt}    /* 14pt */
};

TouhouFont::TouhouFont(Display *display, StandardResolution res)
        : Font(display, res), size(_14PT)
{
        pw = helpers[size].w;
        ph = helpers[size].h;
        image = display->openImage("res/ascii/ascii.png", res);
}

void TouhouFont::print(char character, float x, float y, float z, int align)
{
        DecoratedImage dimage(*image);
        FontSizeHelper *helper = helpers + size;
        int ix = helper->x(character, helper->w, helper->h);
        int iy = helper->y(character, helper->w, helper->h);
        dimage.setXRange(ix, ix + helper->w);
        dimage.setYRange(iy, iy + helper->h);
        display->blitImage(dimage, x, y, w(), h(), 0, z, align);
}

void TouhouFont::setSize(float pound)
{
}
